package cn.endeavour.org.controller;

import cn.endeavour.basic.util.AjaxResult;
import cn.endeavour.basic.util.PageList;
import cn.endeavour.org.domain.Employee;
import cn.endeavour.org.query.EmployeeQuery;
import cn.endeavour.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 这是员工的接口
 * 主要做增删改查
 * Api对接口的描述
 */
@RestController
@RequestMapping("/employee")
@Api(tags = "员工接口",description = "员工接口详细描述")
public class EmployeeController {

    @Autowired
    private IEmployeeService service;

    /**
     * 批量删除
     * @param ids 传回的批量删除的id
     * @return AjaxResult结果,对批量删除操执行的结果
     * produces = "application/json;charset=utf-8"
     */
    @PatchMapping
    @ApiOperation(value = "批量删除",notes = "传入删除所有的参数id")
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        try {
            service.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }
    /**
     * 查询所有 使用Restful分格
     * GetMapping等价于RequestMapping(vaule="", method=REQUESTMETHOD.GET)
     * @return 查询的所有数据
     */
    @GetMapping
    @ApiOperation(value = "查询所有",notes = "哈哈哈哈哈鱼")
    public List<Employee> list() {
        return service.findAll();
    }

    /**
     * 查询单个对象,id是地址栏上的数据,然后通过PathVariable注解进行取值
     * @param id 传入的id
     * @return 单个员工对象
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个",notes = "传入id")
    public Employee findOne(@PathVariable("id") Long id) {
        return service.findById(id);
    }

    /**
     * 查询有多个参数,然后用post 请求,然后用RequestBody在请求体将数据取出
     * @param employeeQuery 前端传入的查询参数
     * @return 总条数以及分页数据
     */
    @PostMapping
    @ApiOperation(value = "分页查询",notes = "传入分页参数")
    public PageList<Employee> query(@RequestBody EmployeeQuery employeeQuery) {
        return service.queryPage(employeeQuery);
    }

    /**
     * 删除一个对象,同样在url获取id
     * @param id 传入的id
     * @return 执行的结果
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "员工删除",notes = "传入id")
    public AjaxResult delete(@PathVariable("id")Long id) {
        try {
            service.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }

    /**
     * 添加或修改
     * @param employee 员工对象
     * @return 执行的结果
     */
    @PutMapping
    @ApiOperation(value = "员工添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult saveOrUpdate(@RequestBody Employee employee) {
        try {
            // 对传入参数进行判断,有id执行修改操作,没有id执行添加操作
            if (employee.getId() == null) {
                service.add(employee);
            } else {
                service.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败");
        }
    }
}
